package sg.partying.lcb.android.ui

import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.king.zxing.util.CodeUtils
import com.salton123.base.ViewBindingActivity
import com.salton123.base.ViewBindingFragment
import com.salton123.base.feature.ImmersionFeature
import com.salton123.coroutine.Ret
import com.salton123.utils.ScreenUtils
import kt.toast
import sg.olaparty.network.viewmodel.NetworkViewModel
import sg.partying.lcb.android.Prop
import sg.partying.lcb.android.R
import sg.partying.lcb.android.databinding.FragmentQrcodeBinding
import sg.partying.lcb.util.ImageLoader

/**
 * Time:2023/11/30 16:36
 * Author:
 * Description:
 */
class QrCodeActivity : ViewBindingActivity<FragmentQrcodeBinding>() {
    private val viewModel by viewModels<NetworkViewModel>()
    override fun getViewBinding(): FragmentQrcodeBinding = FragmentQrcodeBinding.inflate(layoutInflater)

    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)
        ImmersionFeature(this).onBind()
    }
    override fun initViewAndData() {
        viewBind.apply {
            progressCircle.indeterminateDrawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
            ImageLoader.loadCenterCrop(ivThumbnail, Prop.qrCodeTempSavePath)
            Glide.with(ivThumbnail)
                .load(Prop.qrCodeTempSavePath)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .fitCenter()
                .thumbnail(0.3f)
                .into(ivThumbnail)
            tvSkip.setOnClickListener {
                openActivity(HomeActivity::class.java,Bundle())
                finish()
            }
        }
        viewModel.upload(Prop.qrCodeTempSavePath).observe(this) {
            if (it is Ret.Success) {
                toast(getString(com.salton123.translate.R.string.upload_success))
                val qrCodeBitmap = CodeUtils.createQRCode(it.value.msg,
                    ScreenUtils.dp2px(540f), BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
                viewBind.ivQrcode.setImageBitmap(qrCodeBitmap)
                viewBind.progressCircle.visibility = View.GONE
                viewBind.tvSkip.visibility = View.VISIBLE
            } else {
                toast(getString(com.salton123.translate.R.string.upload_failed_please_try_again))
                viewBind.ivQrcode.setImageResource(R.drawable.photo_fail)
                viewBind.progressCircle.visibility = View.GONE
                viewBind.tvSkip.visibility = View.VISIBLE
            }
        }
    }

}